package com.xckj.ibox.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.xckj.ibox.R;
import com.xckj.ibox.databinding.ActivityHomeBinding;
import com.xckj.ibox.event.HideTabBarEvent;
import com.xckj.ibox.event.LoginStateEvent;
import com.xckj.ibox.event.ShowTabBarEvent;
import com.xckj.ibox.framework.BaseActivity;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.ui.fragment.HomeFragment;
import com.xckj.ibox.ui.fragment.MyFragment;
import com.xckj.ibox.ui.fragment.ShoppingCartFragment;
import com.xckj.ibox.ui.fragment.ShoppingMallFragment;
import com.xckj.ibox.ui.fragment.TransactionFragment;
import com.xckj.ibox.utils.AndroidUtil;
import com.xckj.ibox.view.BottomMenuBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @Author: ChenYao
 * @Describe: 首页
 * @Date: 2023/6/12 23:13
 */
public class HomeActivity extends BaseActivity<ActivityHomeBinding> {
    private final Fragment[] mFragment = new Fragment[5];
    private long exitTime = 0;
    private final int indexHome = 0;
    private final int indexTransaction = 1;
    private final int indexShoppingMall = 2;
    private final int indexShoppingCart = 3;
    private final int indexMy = 4;

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    private String getFragmentTag(int index) {
        return "fragment_tab_" + index;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (null != savedInstanceState) {
            Fragment homeTab = getSupportFragmentManager().findFragmentByTag(getFragmentTag(indexHome));
            if (null == homeTab) {
                homeTab = new HomeFragment();
            }
            Fragment transactionTab = getSupportFragmentManager().findFragmentByTag(getFragmentTag(indexTransaction));
            if (null == transactionTab) {
                transactionTab = new TransactionFragment();
            }
            Fragment shoppingMallTab = getSupportFragmentManager().findFragmentByTag(getFragmentTag(indexShoppingMall));
            if (null == shoppingMallTab) {
                shoppingMallTab = new ShoppingMallFragment();
            }
            Fragment shoppingCartTab = getSupportFragmentManager().findFragmentByTag(getFragmentTag(indexShoppingCart));
            if (null == shoppingCartTab) {
                shoppingCartTab = new ShoppingCartFragment();
            }
            Fragment myTab = getSupportFragmentManager().findFragmentByTag(getFragmentTag(indexMy));
            if (null == myTab) {
                myTab = new MyFragment();
            }
            mFragment[indexHome] = homeTab;
            mFragment[indexTransaction] = transactionTab;
            mFragment[indexShoppingMall] = shoppingMallTab;
            mFragment[indexShoppingCart] = shoppingCartTab;
            mFragment[indexMy] = myTab;
        } else {
            mFragment[indexHome] = new HomeFragment();
            mFragment[indexTransaction] = new TransactionFragment();
            mFragment[indexShoppingMall] = new ShoppingMallFragment();
            mFragment[indexShoppingCart] = new ShoppingCartFragment();
            mFragment[indexMy] = new MyFragment();
        }
        dataBinding.bottomTabLayout.setCurrentItem(indexHome);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        EventBus.getDefault().post(new LoginStateEvent());
        if (!AndroidUtil.loginState()) {
            dataBinding.bottomTabLayout.setCurrentItem(indexHome);
        }
    }

    @Override
    protected void setListener() {
        dataBinding.bottomTabLayout.setOnTabSelectedListener(new BottomMenuBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(prePosition, position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }

            @Override
            public boolean onIntercept(int prePosition, int newPosition) {
                return (newPosition == indexTransaction || newPosition == indexShoppingMall || newPosition == indexShoppingCart || newPosition == indexMy) && !AndroidUtil.isLogin();
            }
        });
    }

    private void showHideFragment(int hide, int show) {
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        Fragment showFragment = mFragment[show];
        Fragment hideFragment = mFragment[hide];
        if (hideFragment != showFragment && (null != getSupportFragmentManager().findFragmentByTag(getFragmentTag(hide)))) {
            trx.hide(hideFragment);
        }
        if (!showFragment.isAdded() && null == getSupportFragmentManager().findFragmentByTag(getFragmentTag(show))) {
            trx.add(R.id.container, showFragment, getFragmentTag(show)).commitNowAllowingStateLoss();
        } else {
            trx.show(showFragment).commitNowAllowingStateLoss();
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (System.currentTimeMillis() - exitTime > Constant.EXIT_TIME) {
            Toast.makeText(HomeActivity.this, getString(R.string.back), Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showTabMenu(ShowTabBarEvent showTabBarEvent) {
        dataBinding.bottomTabLayout.setVisibility(View.VISIBLE);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hideTabMenu(HideTabBarEvent hideTabBarEvent) {
        dataBinding.bottomTabLayout.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
